package com.hjxy.controller;

import com.hjxy.pojo.College;
import com.hjxy.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ty
 * @createDate: 2020/9/30
 * @version: 1.0
 */
@Controller
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    //跳转到院系管理页面
    @RequestMapping("/college1")
    public String college(){
        return "college";
    }

    //查询所有院系
    @RequestMapping("/queryCollege")
    @ResponseBody
    public Map<String,Object> queryColleges(){
        List<College> colleges = collegeService.queryCollege();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","成功");
        result.put("colleges",colleges);
        return result;
    }

    //跳转到新增院系页面
    @RequestMapping("/addCollege")
    public String addCollege(){
        return "college-add";
    }

    //新增院系功能
    @RequestMapping("/insertCollege")
    @ResponseBody
    public Map<String,Object> insertCollege(College college){
        int i = collegeService.addCollege(college);
        System.out.println(college.getCollegeId()+"==========id");
        System.out.println(college.getCollegeName()+"=========name");
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","新增成功");
        result.put("college",college);
        return result;
    }

//    //跳转到修改页面
//    @RequestMapping("/update")
//    public String update(Integer collegeId){
//        System.out.println("到这里了嘛");
//        collegeService.queryOneCollegeById(collegeId);
//        System.out.println("这里呢");
//        return "college-update";
//    }
    //修改院系
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> updateCollege(Integer collegeId){
        System.out.println("collegeId1==>"+collegeId);
        College college = collegeService.queryOneCollegeById(collegeId);
        System.out.println("collegeId2==>"+collegeId);
        System.out.println("college==>"+college);
        Map<String,Object> result = new HashMap<>();
        if (college!=null){
            result.put("code",0);
            result.put("message","成功");
            result.put("college",college);
        }else {
            result.put("code",-1);
            result.put("message","失败");
        }
        return result;
    }

    //删除院系功能
    @RequestMapping("/deleted")
    @ResponseBody
    public Map<String,Object> deleteCollege(Integer collegeId){
        collegeService.deleteOneCollege(collegeId);
        System.out.println(collegeId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","删除");
        System.out.println(result);
        return result;
    }
}